//
// Created by 14590 on 25-9-2.
//
#include "cmsis_os2.h"
#include "key_driver.h"

uint8_t Col[5];

uint16_t GetKeyState[8][4], GetKeyStateLast[8][4];
void KeyScan_Task(void *argument)
{
    uint8_t rowid = 1;
    for (;;) {
        HAL_GPIO_WritePin(RowGPIOx, GPIO_PIN_14>>(rowid-1), GPIO_PIN_SET);
        KeyStateLast[rowid] = KeyState[rowid]&0x0F;
        KeyState[rowid] = ((ColGPIOx->IDR&0xF000)>>12)&0x0F;
        for (uint8_t i = 0; i < 4; i++) {
            GetKeyState[rowid][i] = (KeyState[rowid]>>i)&0x01;
            GetKeyStateLast[rowid][i] = (KeyStateLast[rowid]>>i)&0x01;
        }

        for (uint8_t i = 0; i < 4; i++)
            if (GetKeyState[rowid][i] == 1 && GetKeyStateLast[rowid][i] == 0)
                Key[rowid][i].Allowed = 1;

        HAL_GPIO_WritePin(RowGPIOx, GPIO_PIN_14>>(rowid-1), GPIO_PIN_RESET);
        rowid = rowid%7 + 1;
        osDelay(1);
    }
}
